﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sinacor.Infra.Tools;
using System.Web.Script.Serialization;

namespace RightsRegistrationWeb
{
    public partial class Funcao : System.Web.UI.Page
    {
        private string Modulo
        {
            get
            {
                if (!object.ReferenceEquals(Request.QueryString["modulo"], null))
                    return Request.QueryString["modulo"].ToString();
                else
                    return string.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        void LoadDataGrid(string pesquisa, string modulo)
        {
            List<Function> functions = new DataAccess().LoadFunctions(pesquisa, modulo);
            grdFunctions.DataSource = functions;
            grdFunctions.DataBind();
        }

        protected void grdFunctions_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdFunctions.PageIndex = e.NewPageIndex;
            btnPesquisar_Click(btnPesquisar, new EventArgs());
        }

        protected void grdFunctions_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:

                    Function f = (e.Row.DataItem as Function);

                    LinkButton lnkSigla = (LinkButton)e.Row.FindControl("lnkSigla");
                    if (lnkSigla != null)
                    {
                        lnkSigla.Text = f.Sigla;
                        lnkSigla.Attributes.Add("onclick", "ReturnFuncao('" + new JavaScriptSerializer().Serialize(f) + "');");
                    }
                    break;
            }
        }

        protected void btnPesquisar_Click(object sender, EventArgs e)
        {
            LoadDataGrid(txtPesquisa.Text.Trim(), this.Modulo);
            grdFunctions.PageIndex = 0;
        }
    }
}
